/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GPSMapper;

import javax.microedition.midlet.*;
import javax.microedition.location.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;


/**
 * @author thomas.s
 */
public class GPSMapper extends MIDlet implements CommandListener, LocationListener  {
    Command setCmd = new Command ("Aktueller Standpunkt", Command.ITEM, 1);
    Command newCmd = new Command ("Punkt eingeben", Command.ITEM, 1);
    Command testCmd = new Command ("Teststrecke", Command.ITEM, 1);
    Command exitCmd = new Command ("X", Command.EXIT, 2);
    Command okCmd = new Command ("OK", Command.OK, 1);
    Command cancelCmd = new Command ("Zurück", Command.CANCEL, 1);
    GPSMap map = new GPSMap();
    LocationProvider locationProvider = null;
    QualifiedCoordinates c = null;

    TextField iName = null;
    TextField iLat = null;
    TextField iLon = null;


    public GPSMapper()
    {
    }    
    
    public void startApp()
    {
        // set Canvas:
        map.addCommand(setCmd);
        map.addCommand(newCmd);
       // map.addCommand(testCmd);
        map.addCommand(exitCmd);
        map.setCommandListener(this);
        Display.getDisplay(this).setCurrent(map);

        // activate GPS Sensor:
        Criteria crit = new Criteria ();
        crit.setPreferredResponseTime(Criteria.NO_REQUIREMENT);
        crit.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
        crit.setHorizontalAccuracy(100);
        crit.setVerticalAccuracy(100);
        crit.setSpeedAndCourseRequired(true);
        try {
            locationProvider = LocationProvider.getInstance(crit);
            locationProvider.setLocationListener(this, 1, 1, 1);
        }
        catch (LocationException le) { locationProvider = null; }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void close() {
        map = null;
        destroyApp(true);
        notifyDestroyed();
    }

    public void setPoint()
    {
        if (c!=null)
            map.addPoint( c.getLongitude(), c.getLatitude(), c.getAltitude(), "SP" );
    }

    public void newPoint()
    {
        Form f = new Form("Punkt eingeben");
        f.append( iName = new TextField( "Name:", "LE Arena", 40, 0) );
        f.append( iLat = new TextField( "Lat:", "51.3428", 20, TextField.DECIMAL) );
        f.append( iLon = new TextField( "Lon:", "12.3548", 20, TextField.DECIMAL) );
        f.addCommand(okCmd);
        f.addCommand(cancelCmd);
        f.setCommandListener(this);
        Display.getDisplay(this).setCurrent(f);
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCmd) close(); else
        if (c == newCmd) newPoint(); else
        if (c == setCmd) setPoint(); else
        if (c == testCmd)
        {
            map.addPoint(  12.358704, 51.345264, 100, "way");
            map.addPoint(  12.357396, 51.344951, 100, "way");
            map.addPoint(  12.355923, 51.347889, 100, "way");
            map.addPoint(  12.351629, 51.346910, 100, "way");
            map.addPoint(  12.358414, 51.342029, 100, "way");
            

        }
        if (c == okCmd || c == cancelCmd)
        {
            if (iLat!=null && iLon!=null && iName!=null && c==okCmd)
            {
                double lat = Double.parseDouble(iLat.getString());
                double lon = Double.parseDouble(iLon.getString());
                map.addPoint( lon, lat, this.c!=null ? this.c.getAltitude() : 100, iName.getString() );
            }
            iLat=null;
            iLon=null;
             Display.getDisplay(this).setCurrent(map);
        }
    }

    public void locationUpdated( LocationProvider p, final Location loc ) {
        c = loc.getQualifiedCoordinates();
        if (loc.getCourse()!=0) map.course = loc.getCourse();
        map.addPoint( c.getLongitude(), c.getLatitude(), c.getAltitude(), "Way" );
    }

    public void providerStateChanged( LocationProvider p, final int _state ) {
     //   if (p.getState()!=LocationProvider.AVAILABLE) lastUpdate=0;
    }

}
